package com.kws.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.SysConsultantExpert;
import com.kws.system.domain.info.QueryConsultantExpertInfo;
import com.kws.system.domain.info.QueryConsultationRecordInfo;
import com.kws.system.domain.vo.CreateConsultantExpertRq;
import com.kws.system.domain.vo.UpdateConsultantExpertRq;
import com.kws.system.mapper.SysConsultantExpertMapper;
import com.kws.system.service.ExpertCertificateService;
import com.kws.system.service.SysConsultantExpertService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <b>SysConsultantExpertServiceImpl</b>
 *
 * @description: SysConsultantExpertServiceImpl <br>
 * @date: 2024/11/4 16:21 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class SysConsultantExpertServiceImpl extends ServiceImpl<SysConsultantExpertMapper, SysConsultantExpert> implements SysConsultantExpertService {
    private static final Logger logger = LoggerFactory.getLogger(SysConsultantExpertServiceImpl.class);

    @Autowired
    private ExpertCertificateService expertCertificateService;


    @Override
    public Page<QueryConsultantExpertInfo> queryConsultantExpertPage(Integer pageNum, Integer pageSize, String content) {
        Long companyId = SecurityUtils.getCompanyId();
        Page<QueryConsultantExpertInfo> page = new Page<>(pageNum, pageSize);
        Page<QueryConsultantExpertInfo> pageInfo = baseMapper.queryConsultantExpertPage(page,content,companyId);
        return pageInfo;
    }

    @Override
    public boolean createConsultantExpert(CreateConsultantExpertRq rq) {
        SysConsultantExpert sysConsultantExpert = new SysConsultantExpert();
        sysConsultantExpert.setCompanyId(SecurityUtils.getCompanyId());
        sysConsultantExpert.setExpertName(rq.getExpertName());
        sysConsultantExpert.setExpertType(rq.getExpertType());
        sysConsultantExpert.setExpertGrade(rq.getExpertGrade());
        sysConsultantExpert.setExpertQualification(rq.getExpertQualification());
        sysConsultantExpert.setAreaOfExper(rq.getAreaOfExper());
        sysConsultantExpert.setProjectExperience(rq.getProjectExperience());
        sysConsultantExpert.setConsultationStartTime(rq.getConsultationStartTime());
        sysConsultantExpert.setConsultationEndTime(rq.getConsultationEndTime());
        sysConsultantExpert.setPaymentType(rq.getPaymentType());
        sysConsultantExpert.setConsultationsNumber(0);
        Ksys.setBasicModelData(sysConsultantExpert,SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        int insert = baseMapper.insert(sysConsultantExpert);
        if (1 == insert){
            //保存专家证书信息
            expertCertificateService.saveExpertCertificate(sysConsultantExpert.getId(),rq.getFileList());
            return true;
        }
        return false;
    }

    @Override
    public QueryConsultantExpertInfo queryConsultantExpertById(Long id) {
        return baseMapper.queryConsultantExpertById(id);
    }

    @Override
    public boolean updateConsultantExpert(UpdateConsultantExpertRq rq) {
        SysConsultantExpert sysConsultantExpert = baseMapper.selectById(rq.getId());
        sysConsultantExpert.setExpertName(rq.getExpertName());
        sysConsultantExpert.setExpertType(rq.getExpertType());
        sysConsultantExpert.setExpertGrade(rq.getExpertGrade());
        sysConsultantExpert.setExpertQualification(rq.getExpertQualification());
        sysConsultantExpert.setAreaOfExper(rq.getAreaOfExper());
        sysConsultantExpert.setProjectExperience(rq.getProjectExperience());
        sysConsultantExpert.setConsultationStartTime(rq.getConsultationStartTime());
        sysConsultantExpert.setConsultationEndTime(rq.getConsultationEndTime());
        sysConsultantExpert.setPaymentType(rq.getPaymentType());
        Ksys.setBasicModelData(sysConsultantExpert,SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.UPDATE);
        int i = baseMapper.updateById(sysConsultantExpert);
        if (1 == i){
            //修改专家关联证书
            expertCertificateService.updateExpertCertificate(sysConsultantExpert.getId(),rq.getFileIds(),rq.getFileList());
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteConsultantExpertById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_user",SecurityUtils.getStringUserId());
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateConsultantExpertNumber(Long id) {
        SysConsultantExpert sysConsultantExpert = baseMapper.selectById(id);
        sysConsultantExpert.setConsultationsNumber(sysConsultantExpert.getConsultationsNumber()+1);
        int i = baseMapper.updateById(sysConsultantExpert);
        if (1 == i){
            return true;
        }
        return false;
    }

    @Override
    public Page<QueryConsultationRecordInfo> queryConsultationRecordPage(Integer pageNum, Integer pageSize, Long expertId) {
        Page<QueryConsultationRecordInfo> page = new Page<>(pageNum, pageSize);
        Page<QueryConsultationRecordInfo> pageInfo = baseMapper.queryConsultationRecordPage(page,expertId);
        return pageInfo;
    }
}
